<template>
  <a-modal
    title="添加规则"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="结算金额"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input-number style="width: 100%" :precision='2' v-decorator="['amount', {rules: [{required: true, message: '请输入结算金额！'}]}]" />
        </a-form-item>

        <a-form-item
          label="结算日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-date-picker
            show-time
            type="date"
            v-decorator="['settlementDate', {rules: [{required: true, message: '请选择结算日期！'}]}]"
            style="width: 100%"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { apiAddSettlementAmount } from '@/api/shop/settlementAmount'
import AFormItem from 'ant-design-vue/es/form/FormItem'

export default {
  components: { AFormItem },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,

      form: this.$form.createForm(this)
    }
  },
  methods: {
    async add () {
      this.form = this.$form.createForm(this)
      this.visible = true

      this.confirmLoading = true

      this.confirmLoading = false
    },
    handleCancel () {
      this.visible = false
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields(async (errors, values) => {
        if (!errors) {
          const { code, message } = await apiAddSettlementAmount(values)
          this.confirmLoading = false
          if (code === 200) {
            this.$message.success('添加成功')
            this.visible = false
            this.$emit('ok')
          } else {
            this.$message.warning(message)
          }
        } else {
          this.confirmLoading = false
        }
      })
    }
  }
}
</script>
